{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0a1605fb",
   "metadata": {},
   "source": [
    "* 记录人：LZ  \n",
    "* 时间：2022-4-12    \n",
    "* week07: 图像识别 OCR（光学字符识别）   \n",
    "* 学习资源：(文字识别) https://ai.baidu.com/ai-doc/OCR/1k3h7y3db "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97d19122",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\n"
     ]
    }
   ],
   "source": [
    "# 获取access_token\n",
    "\n",
    "import requests\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload ={\n",
    "        'grant_type':'client_credentials',  \n",
    "        'client_id':'5ybZfU6xquae2wppAIIOFC2K',\n",
    "        'client_secret':'vFB0HS9oalKGkP8NcPaZfhxBlDTh6czW'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json()['access_token'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57ef4f12",
   "metadata": {},
   "source": [
    "* 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8aab814c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '令支'}, {'words': '89'}, {'words': '11:18'}, {'words': '搜词条'}, {'words': '中国革命战争的战略问题'}, {'words': '60'}, {'words': '党史百科工作组|本词条由中共党史出版社和党'}, {'words': '白中共党史取社'}, {'words': '国党建读物出社'}, {'words': '建读物出版社提供内容'}, {'words': '毛泽东1936年12月在陕北的红军大学所'}, {'words': '作的演讲。2011年收入中央文献出版社出'}, {'words': '版的《建党以来重要文献选编》第十三'}, {'words': '册。演讲涉及五个问题：一、如何研究战'}, {'words': '争。二、中国共产党与中国革命战争。'}, {'words': '三、中国革命战争的特点。四、“围剿”和'}, {'words': '反“围剿”一中国内战的主要形式。五、战'}, {'words': '略防御。演讲全面总结了土地革命战争时'}, {'words': '期中国共产党领导武装斗争的经验，批判'}, {'words': '了“左”倾冒险主义者在军事上的错误，也'}, {'words': '系统地阐明了有关中国革命战争战略方面'}, {'words': '的诸问题。[1]'}, {'words': '纠错'}, {'words': '参考资料'}, {'words': '编辑'}, {'words': '传视频'}, {'words': '纠错'}, {'words': '60'}], 'words_result_num': 28, 'log_id': 1513718875085492759}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版)\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('word.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95113efd",
   "metadata": {},
   "source": [
    "* 网络图片识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2e1f1d5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '网'}, {'words': '什么'}, {'words': '快乐星球'}], 'words_result_num': 3, 'log_id': 1513720255909568423}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络文字图片识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('web.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e6c4c43",
   "metadata": {},
   "source": [
    "* 手写文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "276b9b6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'location': {'top': 53, 'left': 59, 'width': 462, 'height': 148}, 'words': '你是我奔跑'}, {'location': {'top': 167, 'left': 63, 'width': 494, 'height': 135}, 'words': '也触不到的光'}], 'words_result_num': 2, 'log_id': 1513721047663715908}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "手写文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('write.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e53fb02b",
   "metadata": {},
   "source": [
    "* 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "01c92094",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['微信'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513724916974414562}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('wx.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02413564",
   "metadata": {},
   "source": [
    "* 身份证识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "476bc307",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'失效日期': {'location': {'top': 349, 'left': 495, 'width': 139, 'height': 27}, 'words': '20220118'}, '签发机关': {'location': {'top': 305, 'left': 346, 'width': 163, 'height': 27}, 'words': '柘荣县公安局'}, '签发日期': {'location': {'top': 349, 'left': 342, 'width': 140, 'height': 28}, 'words': '20120118'}}, 'words_result_num': 3, 'image_status': 'normal', 'log_id': 1513726148555146773}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('id.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img,\n",
    "    'id_card_side':'back'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53d9d0fc",
   "metadata": {},
   "source": [
    "* 银行卡识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c1895ae5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'valid_date': '01/25', 'bank_card_number': '6214180701100538758', 'bank_name': '宁波银行', 'bank_card_type': 1, 'holder_name': ''}, 'direction': 0, 'log_id': 1513726676618314946}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('bank.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07bf89a",
   "metadata": {},
   "source": [
    "* 增值税发票识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4c0b610c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '壹佰玖拾捌圆整', 'InvoiceNumConfirm': '56965075', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '98.01980198'}], 'NoteDrawer': '杨梦霞', 'SellerAddress': '百色市右江区城乡路卫校斜对面0776-2869999', 'CommodityNum': [{'row': '1', 'word': '2'}], 'SellerRegisterNum': '91451000552284945M', 'MachineCode': '', 'Remarks': '', 'SellerBank': '广西百色右江农村合作银行银兴支行604112010101357786', 'CommodityTaxRate': [{'row': '1', 'word': '1%'}], 'TotalTax': '1.96', 'InvoiceCodeConfirm': '045001900104', 'CheckCode': '55475185954042453339', 'InvoiceCode': '045001900104', 'InvoiceDate': '2020年06月05日', 'PurchaserRegisterNum': '914501006777146113', 'InvoiceTypeOrg': '广西增值税普通发票', 'OnlinePay': '', 'Password': '>2+-/8736</<-/26-><4536/829-39-275*>628>13*0*->60<4072>2*4484+8<3/9+/3-/639<<*5->*921<1--2-3-<7<8<72*40+9>3<', 'Agent': '否', 'AmountInFiguers': '198.00', 'PurchaserBank': '', 'Checker': '黄瑞丹', 'City': '', 'TotalAmount': '196.04', 'CommodityAmount': [{'row': '1', 'word': '196.04'}], 'PurchaserName': '南宁广发重工发电设备有限责任公司', 'CommodityType': [], 'Province': '广西', 'InvoiceType': '普通发票', 'SheetNum': '第一联：记账联', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '1.96'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '间'}], 'Payee': '宁玉梅', 'CommodityName': [{'row': '1', 'word': '住宿服务*住宿费'}], 'SellerName': '百色市豪瑞酒店有限公司', 'InvoiceNum': '56965075'}, 'log_id': 1513727338866730607}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('sui.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b50217b6",
   "metadata": {},
   "source": [
    "* 火车票识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "26b848d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 13, 'words_result': {'id_num': '2302051998****156X', 'serial_number': '10010301110403F067846', 'ticket_num': 'F067846', 'seat_num': '02车03C号', 'time': '09:36', 'date': '2019年04月03日', 'name': '裴瑜丽', 'destination_station': '天津站', 'seat_category': '二等座', 'starting_station': '北京南站', 'sales_station': '北京南', 'train_num': 'C2565', 'ticket_rates': '￥54.5元'}, 'direction': 0, 'log_id': 1513727778462165740}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "\n",
    "f = open('huo.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':\"24.d8eee07750a9e7deb627283822ed3d92.2592000.1652325064.282335-25948716\",\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ade71bd1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
